package com.the4man.cnbdw.utils;

import com.the4man.framework.utils.DateJsonValueProcessor;
import com.the4man.framework.utils.JsonWrapper;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by lenovo on 2017/12/12.
 */
public class BaseForm<T> {
    private String id;
    private String ids;
    private Page<T> pageSource = new Page(this);
    private Integer curPage = Integer.valueOf(1);
    private Integer rowsPerPage = Integer.valueOf(15);
    private String to;
    private String stp;
    private String stp2;
    private String popMessage;
    private boolean resultBoolean;
    private String jsonText;

    public BaseForm() {
    }

    public String getJsonText() {
        return this.jsonText;
    }

    public void setJsonText(String jsonText) {
        this.jsonText = jsonText;
    }

    public boolean isEntrance() {
        return this.stp == null || "".equals(this.stp);
    }

    public boolean isStep(String stepName) {
        return stepName.equals(this.stp);
    }

    public Page<T> getPageSource() {
        return this.pageSource;
    }

    public String getStp() {
        return this.stp;
    }

    public void setStp(String stp) {
        this.stp = stp;
    }

    public String getTo() {
        return this.to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void setPageSource(Page<T> pageSource) {
        this.pageSource = pageSource;
    }

    public Integer getRowsPerPage() {
        return this.rowsPerPage;
    }

    public void setRowsPerPage(Integer rowPerPage) {
        if(rowPerPage != null && rowPerPage.intValue() >= 0) {
            this.rowsPerPage = rowPerPage;
            this.pageSource.setRowsPerPage(rowPerPage.intValue());
        } else {
            this.rowsPerPage = Integer.valueOf(this.pageSource.getRowsPerPage());
        }

    }

    public Integer getCurPage() {
        return this.curPage;
    }

    public void setCurPage(Integer curPage) {
        if(curPage == null || curPage.intValue() < 0) {
            curPage = Integer.valueOf(1);
        }

        this.curPage = curPage;
        this.pageSource.setCurPage(curPage.intValue());
    }

    public static void main(String[] args) {
        int y = Calendar.getInstance().get(1);
        int m = Calendar.getInstance().get(2);
        System.out.println(y + ":" + m);
    }

    public String getStp2() {
        return this.stp2;
    }

    public void setStp2(String stp2) {
        this.stp2 = stp2;
    }

    public void setJsonMsg(Object data) {
        this.setJsonMsg(this.popMessage, this.resultBoolean, data);
    }

    public void setJsonMsg(String msg, boolean flag, Object data) {
        JsonWrapper wrapper = new JsonWrapper(flag, msg, data);
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
        this.setJsonText(JSONObject.fromObject(wrapper, config).toString());
    }

    public void setJsonMsg() {
        this.setJsonMsg(this.popMessage, this.resultBoolean, (Object)null);
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIds() {
        return this.ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }
}
